import { createStore } from 'vuex'

const store = createStore({
    state:{
        showLogin: false,
        token:localStorage.getItem("token"),
        username:localStorage.getItem("username")
    },
    mutations:{
        SET_TOKEN:(state,token)=>{
            state.token=token;
            localStorage.setItem("token",token);
        },
        SET_SHOW_LOGIN:(state,value)=>{
            state.showLogin = value;
        },
        SET_USERNAME:(state,username)=>{
            state.username=username;
            localStorage.setItem("username",username);

        },
        REMOVE:(state)=>{
            state.token='';
            state.username='';
            localStorage.setItem("token",'');
            localStorage.setItem("username",'');
        }
    },
    getters:{
        getToken:state => {
            if(state.token ===null){
                return '';
            }
            else {
                return state.token;
            }
        },
        getUsername:state => {
            if(state.username ===null){
                return '';
            }
            else {
                return state.username;
            }
        }
    }
});
// 暴露仓库
export default store;